﻿namespace ManagerClinic.DanhMuc
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmDmGhiChuCachDung : DevExpress.XtraEditors.XtraForm
    {
        GridCheckMarksSelection gridCheckMarksSelection = null;
        IList<DmGhiChuCachDungEntity> lstDmGhiChuCachDungEntity = null;
        DmGhiChuCachDungBussiness objdmghichucachdung = new DmGhiChuCachDungBussiness();

        public frmDmGhiChuCachDung()
        {
            InitializeComponent();
            gridCheckMarksSelection = new GridCheckMarksSelection(gvDmGhichucachdung);
        }

        void LoadData(object sender, EventArgs e)
        {
            lstDmGhiChuCachDungEntity = objdmghichucachdung.GetDmGhiChuCachDungList();
            grdDmGhiChuCachdung.DataSource = lstDmGhiChuCachDungEntity;
        }

        DmGhiChuCachDungEntity GetRow()
        {
            if (gvDmGhichucachdung.RowCount > 0)
                return (DmGhiChuCachDungEntity)gvDmGhichucachdung.GetFocusedRow();
            return null;
        }

        private void frmDMGhiChuCachDung_Load(object sender, EventArgs e)
        {
            Authenticate.IsAuthenticateForm(this);
            LoadData(sender, e);
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            var frm = new frmCapNhatDmGhiChuCachDung((int)ConstantManagerClinic.StateInsertOrUpdate.Insert);
            frm.dmGhiChuCachDungEvent += new EventHandler(LoadData);
            frm.ShowDialog();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            DmGhiChuCachDungEntity dmch = GetRow();
            if (dmch == null) return;
            var frm = new frmCapNhatDmGhiChuCachDung((int)ConstantManagerClinic.StateInsertOrUpdate.Update, dmch);
            frm.dmGhiChuCachDungEvent += new EventHandler(LoadData);
            frm.ShowDialog();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void grdDmGhiChuCachdung_DoubleClick(object sender, EventArgs e)
        {
            if (btnSua.Enabled == false) return;
            btnSua_Click(sender, e);
        }

        private void gvDmGhichucachdung_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
                e.Handled = false;
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            var deleteObjects = gridCheckMarksSelection.selection;
            if (deleteObjects.Count <= 0)
            {
                MessageBox.Show("Bạn chưa chọn bản ghi để xóa!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK,MessageBoxIcon.Information);
                return;
            }

            if (MessageBox.Show("Bạn chắc chắn muốn xóa dữ liệu?", ConstantManagerClinic.sTitleWarming, MessageBoxButtons.YesNo,MessageBoxIcon.Warning) == DialogResult.No)
            {
                gridCheckMarksSelection.ClearSelection();
                return;
            }
            foreach (DmGhiChuCachDungEntity item in deleteObjects)
            {
                objdmghichucachdung.DeleteDmGhiChuCachDung(item.MaGhiChuCachDung);
            }
            gridCheckMarksSelection.ClearSelection();
            LoadData(sender, e);
        }

    }
}